<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开门ID" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入开门ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="machineId">
        <el-input
          v-model="queryParams.machineId"
          placeholder="请输入设备ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="machineCode">
        <el-input
          v-model="queryParams.machineCode"
          placeholder="请输入设备编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开门方式，1000 人脸开门 2000 钥匙开门" prop="openTypeCd">
        <el-input
          v-model="queryParams.openTypeCd"
          placeholder="请输入开门方式，1000 人脸开门 2000 钥匙开门"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否带帽 0没有带 1 带帽" prop="hat">
        <el-input
          v-model="queryParams.hat"
          placeholder="请输入是否带帽 0没有带 1 带帽"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="相似度" prop="similarity">
        <el-input
          v-model="queryParams.similarity"
          placeholder="请输入相似度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="欠费金额" prop="amountOwed">
        <el-input
          v-model="queryParams.amountOwed"
          placeholder="请输入欠费金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->
      <div class="filter-container" style="margin-bottom:10px">
          <el-input
                  v-model="queryParams.machineName"
                  placeholder="请输入门禁名称"
                  style="width: 200px;"
                  class="filter-item"
          />
          <el-input
                  v-model="queryParams.name"
                  placeholder="请输入用户名称"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-input
                  v-model="queryParams.machineCode"
                  placeholder="请输入门禁编码"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-button
                  v-waves
                  class="filter-item"
                  type="primary"
                  icon="el-icon-search"
                  @click="getMachineOpenDoors"
          >查询</el-button>
      </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['access:door:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['access:door:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['access:door:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['access:door:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--<el-table v-loading="loading" :data="doorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="开门ID" align="center" prop="openId" />
      <el-table-column label="设备ID" align="center" prop="machineId" />
      <el-table-column label="设备编码" align="center" prop="machineCode" />
      <el-table-column label="开门方式，1000 人脸开门 2000 钥匙开门" align="center" prop="openTypeCd" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="是否带帽 0没有带 1 带帽" align="center" prop="hat" />
      <el-table-column label="模板人脸" align="center" prop="modelFace" />
      <el-table-column label="抓拍图片" align="center" prop="face" />
      <el-table-column label="相似度" align="center" prop="similarity" />
      <el-table-column label="欠费金额" align="center" prop="amountOwed" />
      <el-table-column label="小区ID" align="center" prop="communityId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['access:door:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['access:door:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>-->

      <el-table
              v-loading="loading"
              :data="doorList"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row
      >
          <el-table-column align="center" label="编号" width="90">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
          </el-table-column>
          <el-table-column align="center" label="模板人脸">
              <template slot-scope="scope">
                  <el-image
                          ref="lazyImg"
                          lazy
                          class="vx-lazyLoad"
                          :src="scope.row.modelFace"
                          :fit="fit"
                          :preview-src-list="[scope.row.modelFace]"
                  >
                      <div slot="placeholder" class="image-slot">
                          <i class="el-icon-loading"></i>加载中
                      </div>
                      <div slot="error" class="image-slot">
                          <i class="el-icon-picture-outline"></i>
                      </div>
                  </el-image>
              </template>
          </el-table-column>
          <el-table-column align="center" label="抓拍">
              <template slot-scope="scope">
                  <el-image
                          ref="lazyImg"
                          lazy
                          class="vx-lazyLoad"
                          :src="scope.row.face"
                          :fit="fit"
                          :preview-src-list="[scope.row.face]"
                  >
                      <div slot="placeholder" class="image-slot">
                          <i class="el-icon-loading"></i>加载中
                      </div>
                      <div slot="error" class="image-slot">
                          <i class="el-icon-picture-outline"></i>
                      </div>
                  </el-image>
              </template>
          </el-table-column>
          <el-table-column align="center" label="用户名称">
              <template slot-scope="scope">{{ scope.row.userName }}</template>
          </el-table-column>
<!--          <el-table-column align="center" label="开门方式">
              <template slot-scope="scope">{{ scope.row.openTypeCdName }}</template>
          </el-table-column>
          <el-table-column align="center" label="带帽">
              <template slot-scope="scope">{{ scope.row.hatName }}</template>
          </el-table-column>-->
          <el-table-column align="center" label="相似度">
              <template slot-scope="scope">
                  <el-tag v-if="scope.row.similarity > 0" type="success">{{ scope.row.similarity }}</el-tag>
                  <el-tag v-else type="danger">开门失败</el-tag>
              </template>
          </el-table-column>
<!--          <el-table-column align="center" label="欠费情况">
              <template slot-scope="scope">{{ scope.row.amountOwed }}</template>
          </el-table-column>-->
          <el-table-column align="center" label="门禁名称">
              <template slot-scope="scope">{{ scope.row.machineName }}</template>
          </el-table-column>
          <el-table-column label="门禁编码" align="center">
              <template slot-scope="scope">
                  <span>{{ scope.row.machineCode }}</span>
              </template>
          </el-table-column>
          <el-table-column label="门禁IP" align="center">
              <template slot-scope="scope">
                  <span>{{ scope.row.machineIp }}</span>
              </template>
          </el-table-column>
          <el-table-column class-name="status-col" label="开门时间" align="center">
              <template slot-scope="scope">{{ scope.row.createTime }}</template>
          </el-table-column>
          <el-table-column class-name="status-col" label="操作" align="center">
              <template slot-scope="scope">
                  <el-button @click="openMonitor(scope.row)">监控</el-button>
              </template>
          </el-table-column>
      </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改开门记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="开门ID" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入开门ID" />
        </el-form-item>
        <el-form-item label="设备ID" prop="machineId">
          <el-input v-model="form.machineId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="设备编码" prop="machineCode">
          <el-input v-model="form.machineCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="开门方式，1000 人脸开门 2000 钥匙开门" prop="openTypeCd">
          <el-input v-model="form.openTypeCd" placeholder="请输入开门方式，1000 人脸开门 2000 钥匙开门" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="是否带帽 0没有带 1 带帽" prop="hat">
          <el-input v-model="form.hat" placeholder="请输入是否带帽 0没有带 1 带帽" />
        </el-form-item>
        <el-form-item label="模板人脸" prop="modelFace">
          <el-input v-model="form.modelFace" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="抓拍图片" prop="face">
          <el-input v-model="form.face" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="相似度" prop="similarity">
          <el-input v-model="form.similarity" placeholder="请输入相似度" />
        </el-form-item>
        <el-form-item label="欠费金额" prop="amountOwed">
          <el-input v-model="form.amountOwed" placeholder="请输入欠费金额" />
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDoor, getDoor, delDoor, addDoor, updateDoor, exportDoor } from "@/api/access/door";

export default {
  name: "Door",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开门记录表格数据
      doorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openId: null,
        machineId: null,
        machineCode: null,
        openTypeCd: null,
        userId: null,
        userName: null,
        hat: null,
        modelFace: null,
        face: null,
        similarity: null,
        amountOwed: null,
        communityId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openId: [
          { required: true, message: "开门ID不能为空", trigger: "blur" }
        ],
        machineId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        machineCode: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        openTypeCd: [
          { required: true, message: "开门方式，1000 人脸开门 2000 钥匙开门不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        communityId: [
          { required: true, message: "小区ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询开门记录列表 */
    getList() {
      this.loading = true;
      listDoor(this.queryParams).then(response => {
        this.doorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        openId: null,
        machineId: null,
        machineCode: null,
        openTypeCd: null,
        userId: null,
        userName: null,
        hat: null,
        modelFace: null,
        face: null,
        similarity: null,
        amountOwed: null,
        createTime: null,
        communityId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.openId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加开门记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const openId = row.openId || this.ids
      getDoor(openId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开门记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.openId != null) {
            updateDoor(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDoor(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const openIds = row.openId || this.ids;
      this.$confirm('是否确认删除开门记录编号为"' + openIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDoor(openIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有开门记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDoor(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
